# -*- coding: utf-8 -*-
"""
Created on Tue Aug 10 13:49:02 2021

@author: xiong
"""
import arcpy
import arcpy.mapping as mp
import time
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


'运行前备份图斑跟点shp'



def writelog(str0):
    localtime = time.asctime( time.localtime(time.time()) )
    f = open(workspace +'\\log.txt','a')
    f.write(localtime +':' + str0+'\n' )
    f.close()
    

workspace = r'E:\tutu\123'
name =  r'973_刺芒野古草草丛'


belongtbList =  [[] for i in range(8)] 
tb0dic = {}
tb0List = []
polygonPath = workspace + '\\基础' + '\\' + name + '_图斑.shp'
pointPath = workspace + '\\基础' + '\\' + name + '.shp'
try:
    os.mkdir((workspace + '\\空间分布').decode('utf-8'))
    os.mkdir((workspace + '\\温度带').decode('utf-8'))
except:
    pass

temprlist = ['温度交错带','高山极地带','苔原带','寒温带','中温带','暖温带','亚热带','热带']


writelog('开始运行')

with arcpy.da.UpdateCursor(polygonPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
    for row in cursor:
        if int(row[7]) in [1,2,3,4,5,6,7]:
            belongtbList[int(row[7])].append(row)
            continue
        if int(row[5]) not in tb0List:
            tb0dic[str(int(row[5]))] = [row]
            tb0List.append(int(row[5]))
        else:
            tb0dic[str(int(row[5]))] .append(row)
            
    writelog('记录图斑信息结束')

with arcpy.da.UpdateCursor(polygonPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
    for row in cursor:

        cursor.deleteRow()
    writelog('图斑已清空')
  
writelog('开始处理非温度交错带图斑')
count = 0 
for rows in belongtbList:
    if len(rows)>0:
        for t in range(0,len(rows)):
           with arcpy.da.InsertCursor(polygonPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
               cursor.insertRow(rows[t])
               
        arcpy.SelectLayerByAttribute_management(name,"NEW_SELECTION",'"tb"> 0')
        arcpy.SelectLayerByLocation_management(name,"COMPLETELY_WITHIN",polygonPath)
        newpath =(workspace + '\\温度带' + '\\%s_%s'%(str(count),temprlist[count])).decode('utf-8')
        os.mkdir(newpath)
        newpath1   = newpath + '\\基础'
        newpath2   = newpath + '\\csv'
        newpath3   = newpath + '\\不压点有序度图'
        os.mkdir(newpath1)
        os.mkdir(newpath2)
        os.mkdir(newpath3)
        arcpy.CopyFeatures_management(polygonPath,newpath1 + '\\' + name + '_' + temprlist[count] + '_图斑.shp') #114_虎榛子灌丛_寒温带_图斑.shp
        arcpy.CopyFeatures_management(name,newpath1 + '\\' + name + '_' + temprlist[count] + '.shp') #114_虎榛子灌丛_寒温带.shp
            
        with arcpy.da.UpdateCursor(polygonPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
            for row in cursor:
                cursor.deleteRow()
        writelog(temprlist[count] + '结束')
    elif count > 0 :
        writelog(temprlist[count] + '无图斑')
    count += 1
writelog('处理非温度交错带图斑完毕')            

if tb0List:    
    os.mkdir((workspace + '\\温度带' + '\\0_温度交错带').decode('utf-8'))   
    writelog('开始处理温度交错带图斑')
    for i in tb0List:
        for t in range(0,len(tb0dic[str(i)])):
           with arcpy.da.InsertCursor(polygonPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
               cursor.insertRow(tb0dic[str(i)][t])
        
        arcpy.SelectLayerByAttribute_management(name,"NEW_SELECTION",'"tb"> 0')
        arcpy.SelectLayerByLocation_management(name,"COMPLETELY_WITHIN",polygonPath)
        newname = '.'.join(e for e in str(i)) + '_' + '-' .join(temprlist[int(e)] for e in str(i)) + '交错带' #3.4.5_寒温带-中温带-暖温带交错带
        newpath =(workspace + '\\温度带' + '\\0_温度交错带' + '\\' + newname).decode('utf-8')
        os.mkdir(newpath)
        newpath1   = newpath + '\\基础'
        newpath2   = newpath + '\\csv'
        newpath3   = newpath + '\\不压点有序度图'
        os.mkdir(newpath1)
        os.mkdir(newpath2)
        os.mkdir(newpath3)
        arcpy.CopyFeatures_management(polygonPath,newpath1 + '\\' + name + '_' + '' .join(temprlist[int(e)] for e in str(i)) + '交错带' + '_图斑.shp') #带-号会报错
        arcpy.CopyFeatures_management(name,newpath1 + '\\' + name + '_' + '' .join(temprlist[int(e)] for e in str(i)) + '交错带' + '.shp') 
        with arcpy.da.UpdateCursor(polygonPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
            for row in cursor:
                cursor.deleteRow()
        writelog('温度交错带' + str(i) + '结束')
        
writelog('数据写回原图斑')             
cntr3 = 0
with arcpy.da.InsertCursor(polygonPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
    for i in belongtbList: 
        if i :
            for row in i:
                cursor.insertRow(row)
                cntr3 += 1
with arcpy.da.InsertCursor(polygonPath,('FID',"Shape@",'type','name','value','real_TB','rate','belong_TB','区域','小区域','类型')) as cursor:
    for i in tb0List: 
        for row in tb0dic[str(i)]:
            cursor.insertRow(row)
            cntr3 += 1
writelog(str(cntr3))                   
